Option Strict On
Option Explicit On 

Public Class ExpressionControl
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Splitter2 As System.Windows.Forms.Splitter
    Friend WithEvents txtSort As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(ExpressionControl))
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.Splitter1 = New System.Windows.Forms.Splitter
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Splitter2 = New System.Windows.Forms.Splitter
        Me.txtSort = New System.Windows.Forms.TextBox
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.AccessibleDescription = Nothing
        Me.TreeView1.AccessibleName = Nothing
        resources.ApplyResources(Me.TreeView1, "TreeView1")
        Me.TreeView1.BackgroundImage = Nothing
        Me.TreeView1.Font = Nothing
        Me.TreeView1.HideSelection = False
        Me.TreeView1.ImageList = Me.ImageList1
        Me.TreeView1.ItemHeight = 16
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {CType(resources.GetObject("TreeView1.Nodes"), System.Windows.Forms.TreeNode)})
        '
        'ImageList1
        '
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        Me.ImageList1.Images.SetKeyName(0, "")
        Me.ImageList1.Images.SetKeyName(1, "")
        '
        'Splitter1
        '
        Me.Splitter1.AccessibleDescription = Nothing
        Me.Splitter1.AccessibleName = Nothing
        resources.ApplyResources(Me.Splitter1, "Splitter1")
        Me.Splitter1.BackgroundImage = Nothing
        Me.Splitter1.Font = Nothing
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.TabStop = False
        '
        'ListBox1
        '
        Me.ListBox1.AccessibleDescription = Nothing
        Me.ListBox1.AccessibleName = Nothing
        resources.ApplyResources(Me.ListBox1, "ListBox1")
        Me.ListBox1.BackgroundImage = Nothing
        Me.ListBox1.Font = Nothing
        Me.ListBox1.Name = "ListBox1"
        '
        'TextBox1
        '
        Me.TextBox1.AccessibleDescription = Nothing
        Me.TextBox1.AccessibleName = Nothing
        resources.ApplyResources(Me.TextBox1, "TextBox1")
        Me.TextBox1.BackgroundImage = Nothing
        Me.TextBox1.Font = Nothing
        Me.TextBox1.HideSelection = False
        Me.TextBox1.Name = "TextBox1"
        '
        'Splitter2
        '
        Me.Splitter2.AccessibleDescription = Nothing
        Me.Splitter2.AccessibleName = Nothing
        resources.ApplyResources(Me.Splitter2, "Splitter2")
        Me.Splitter2.BackgroundImage = Nothing
        Me.Splitter2.Cursor = System.Windows.Forms.Cursors.HSplit
        Me.Splitter2.Font = Nothing
        Me.Splitter2.Name = "Splitter2"
        Me.Splitter2.TabStop = False
        '
        'txtSort
        '
        Me.txtSort.AccessibleDescription = Nothing
        Me.txtSort.AccessibleName = Nothing
        resources.ApplyResources(Me.txtSort, "txtSort")
        Me.txtSort.BackgroundImage = Nothing
        Me.txtSort.Font = Nothing
        Me.txtSort.HideSelection = False
        Me.txtSort.Name = "txtSort"
        '
        'GroupBox1
        '
        Me.GroupBox1.AccessibleDescription = Nothing
        Me.GroupBox1.AccessibleName = Nothing
        resources.ApplyResources(Me.GroupBox1, "GroupBox1")
        Me.GroupBox1.BackgroundImage = Nothing
        Me.GroupBox1.Controls.Add(Me.TextBox1)
        Me.GroupBox1.Font = Nothing
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.TabStop = False
        '
        'ExpressionControl
        '
        Me.AccessibleDescription = Nothing
        Me.AccessibleName = Nothing
        resources.ApplyResources(Me, "$this")
        Me.BackgroundImage = Nothing
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Splitter1)
        Me.Controls.Add(Me.TreeView1)
        Me.Controls.Add(Me.Splitter2)
        Me.Controls.Add(Me.GroupBox1)
        Me.Name = "ExpressionControl"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mExpressionType As Model10.ExpressionType
    Private mTable As Model10.Table

    Public Event ValueChenged As EventHandler

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        If Not ListBox1.SelectedItem Is Nothing Then
            Dim s As Integer = TextBox1.SelectionStart
            TextBox1.Text = TextBox1.Text.Remove(s, TextBox1.SelectionLength).Insert(s, " " & ListBox1.SelectedItem.ToString & " ")
            TextBox1.SelectionStart = s + ListBox1.SelectedItem.ToString.Length + 2
            TextBox1.Select()
        End If
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        RaiseEvent ValueChenged(Me, New EventArgs)
    End Sub
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        ListBox1.Items.Clear()
        Select Case TreeView1.SelectedNode.Text
            Case "All"
                Select Case mExpressionType
                    Case ExpressionType.Filter
                        Column(mTable)
                        Main()
                        Aggregate()
                        Constant()
                        Arithmetic()
                        Comparison()
                        Logical()
                    Case ExpressionType.Sort
                        Column(mTable)
                        SortOrder()
                End Select
            Case "Columns" : Column(mTable)
            Case "Sort Order" : SortOrder()
            Case "Functions"
                Main()
                Aggregate()
            Case "Main" : Main()
            Case "Aggregate" : Aggregate()
            Case "Constants" : Constant()
            Case "Operators"
                Arithmetic()
                Comparison()
                Logical()
            Case "Arithmetic" : Arithmetic()
            Case "Comparison" : Comparison()
            Case "Logical" : Logical()
        End Select
    End Sub

    Private Sub SortOrder()
        ListBox1.Items.Add("ASC")
        ListBox1.Items.Add("DESC")
    End Sub
    Private Sub Column(ByVal tbl As Model10.Table)
        Dim dc As DataColumn
        For Each dc In tbl.DataTable.Columns
            If Not dc.ColumnName = tbl.Field Then ListBox1.Items.Add("[" & dc.ColumnName & "]")
        Next : dc = Nothing
    End Sub
    Private Sub Main()
        ListBox1.Items.Add("CONVERT(, )")
        ListBox1.Items.Add("LEN()")
        ListBox1.Items.Add("ISNULL(, )")
        ListBox1.Items.Add("IIF(, , )")
        ListBox1.Items.Add("TRIM()")
        ListBox1.Items.Add("SUBSTRING(, , )")
    End Sub
    Private Sub Aggregate()
        ListBox1.Items.Add("Sum()")
        ListBox1.Items.Add("Avg()")
        ListBox1.Items.Add("Min()")
        ListBox1.Items.Add("Max()")
        ListBox1.Items.Add("Count()")
        ListBox1.Items.Add("StDev()")
        ListBox1.Items.Add("Var()")
    End Sub
    Private Sub Constant()
        ListBox1.Items.Add("''")
        ListBox1.Items.Add("False")
        ListBox1.Items.Add("Null")
        ListBox1.Items.Add("True")
    End Sub
    Private Sub Arithmetic()
        ListBox1.Items.Add("+")
        ListBox1.Items.Add("-")
        ListBox1.Items.Add("*")
        ListBox1.Items.Add("/")
        ListBox1.Items.Add("\")
        ListBox1.Items.Add("^")
        ListBox1.Items.Add("Mod")
        ListBox1.Items.Add("%")
    End Sub
    Private Sub Comparison()
        ListBox1.Items.Add("<")
        ListBox1.Items.Add(">")
        ListBox1.Items.Add("<=")
        ListBox1.Items.Add(">=")
        ListBox1.Items.Add("<>")
        ListBox1.Items.Add("=")
        ListBox1.Items.Add("BETWEEN")
        ListBox1.Items.Add("IN")
        ListBox1.Items.Add("LIKE")
    End Sub
    Private Sub Logical()
        ListBox1.Items.Add("And")
        ListBox1.Items.Add("Eqv")
        ListBox1.Items.Add("Imp")
        ListBox1.Items.Add("Not")
        ListBox1.Items.Add("Or")
        ListBox1.Items.Add("Xor")
    End Sub

    Private Sub Filter()
        GroupBox1.Text = GetSystemResource("Expression.FilterExpression")
        TreeView1.Nodes.Clear()
        TreeView1.Nodes.AddRange(New TreeNode() {New TreeNode("All", New TreeNode() {New TreeNode("Columns"), New TreeNode("Functions", New TreeNode() {New TreeNode("Main"), New TreeNode("Aggregate")}), New TreeNode("Constants"), New TreeNode("Operators", New TreeNode() {New TreeNode("Arithmetic"), New TreeNode("Comparison"), New TreeNode("Logical")})})})
    End Sub
    Private Sub Sort()
        GroupBox1.Text = GetSystemResource("Expression.SortExpression")
        TreeView1.Nodes.Clear()
        TreeView1.Nodes.AddRange(New TreeNode() {New TreeNode("All", New TreeNode() {New TreeNode("Columns"), New TreeNode("Sort Order")})})
    End Sub

    Public Property ExpressionType() As Model10.ExpressionType
        Get
            Return mExpressionType
        End Get
        Set(ByVal Value As Model10.ExpressionType)
            mExpressionType = Value
        End Set
    End Property
    Public Property Expression() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal Value As String)
            TextBox1.Text = Value
        End Set
    End Property
    Public Property Table() As Model10.Table
        Get
            Return mTable
        End Get
        Set(ByVal Value As Model10.Table)
            mTable = Value
        End Set
    End Property

    Public Sub Fill()
        Select Case mExpressionType
            Case ExpressionType.Filter : Filter()
            Case ExpressionType.Sort : Sort()
        End Select
        TreeView1.Nodes(0).Expand()
        TreeView1.Nodes(0).Nodes(0).Expand()
        TreeView1.SelectedNode = TreeView1.Nodes(0).Nodes(0)
    End Sub
    Public Sub New(ByVal expressionType As Model10.ExpressionType, ByVal table As Model10.Table)
        MyBase.New()
        InitializeComponent()

        mExpressionType = expressionType
        mTable = table
        Fill()
    End Sub
End Class
